<template>
  <!-- 新建产品规格 -->
  <Dialog
    :title="dialogTitle"
    v-model="formDialogVisible"
    :append-to-body="false"
    :lock-scroll="false"
    class="enhanced-dialog"
  >
    <!-- 弹窗头部 -->
    <div class="dialog-header">
      <div class="header-icon">
        <Icon icon="ep:setting" />
      </div>
      <div class="header-content">
        <h4 class="dialog-title">{{ dialogTitle }}</h4>
        <p class="dialog-subtitle">产品规格配置</p>
      </div>
    </div>

    <!-- 表单内容 -->
    <div class="form-container">
      <el-form
        ref="skuFormRef"
        :model="skuFormData"
        :rules="skuFormRules"
        label-width="100px"
        v-loading="skuFormLoading"
        class="enhanced-form"
      >
        <!-- 产品信息 -->
        <div class="form-section">
          <div class="section-title">
            <Icon icon="ep:info-filled" class="section-icon" />
            <span>产品信息</span>
          </div>

          <el-form-item label="产品名称:" prop="name" class="form-item info-item">
            <div class="info-display">
              <Icon icon="ep:goods" class="info-icon" />
              <span class="info-text">{{ productObj.name }}</span>
            </div>
          </el-form-item>

          <el-form-item label="产品单价:" prop="unitPrice" class="form-item info-item">
            <div class="info-display">
              <Icon icon="ep:money" class="info-icon" />
              <span class="info-text price-text">¥{{ productObj.unitPrice }}</span>
            </div>
          </el-form-item>
        </div>

        <!-- 规格配置 -->
        <div class="form-section">
          <div class="section-title">
            <Icon icon="ep:setting" class="section-icon" />
            <span>规格配置</span>
          </div>

          <el-form-item label="规格类型" prop="type" class="form-item">
            <el-radio-group
              @change="typeChange"
              v-model="skuFormData.type"
              class="enhanced-radio-group"
            >
              <el-radio :label="1" class="enhanced-radio">
                <Icon icon="ep:collection" class="radio-icon" />
                指定数量
              </el-radio>
              <el-radio :label="2" class="enhanced-radio">
                <Icon icon="ep:edit" class="radio-icon" />
                自定义数量
              </el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item v-if="skuFormData.type == 1" label="规格数量" prop="num" class="form-item">
            <el-input
              v-model="skuFormData.num"
              @change="numChange"
              placeholder="请输入规格数量"
              class="enhanced-input"
            >
              <template #prefix>
                <Icon icon="ep:collection" />
              </template>
            </el-input>
          </el-form-item>

          <el-form-item v-if="skuFormData.type == 1" label="原价" prop="oldPrice" class="form-item">
            <el-input
              v-model="skuFormData.oldPrice"
              placeholder="请输入原价"
              class="enhanced-input"
            >
              <template #prefix>
                <Icon icon="ep:money" />
              </template>
            </el-input>
          </el-form-item>

          <el-form-item label="单价" prop="price" class="form-item">
            <el-input v-model="skuFormData.price" placeholder="请输入单价" class="enhanced-input">
              <template #prefix>
                <Icon icon="ep:money" />
              </template>
            </el-input>
          </el-form-item>

          <el-form-item label="有效期" prop="expireTime" class="form-item">
            <el-input
              v-model="skuFormData.expireTime"
              placeholder="请输入有效期（单位天，0表示永久）"
              class="enhanced-input"
            >
              <template #prefix>
                <Icon icon="ep:timer" />
              </template>
            </el-input>
          </el-form-item>
        </div>
      </el-form>
    </div>

    <!-- 弹窗底部 -->
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="submitForm" type="primary" :disabled="skuFormLoading" class="submit-btn">
          <Icon icon="ep:check" class="btn-icon" />
          确 定
        </el-button>
        <el-button @click="formDialogVisible = false" class="cancel-btn">
          <Icon icon="ep:close" class="btn-icon" />
          取 消
        </el-button>
      </div>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import { getProduct } from '@/api/system/product/index'
import * as ProductSkuApi from '@/api/system/productsku'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const formDialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const skuFormLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const productObj = ref({
  name: undefined,
  unitPrice: undefined
})
const skuFormData = ref({
  productId: undefined,
  type: undefined,
  num: undefined,
  oldPrice: undefined,
  price: undefined,
  expireTime: undefined
})
const skuFormRules = reactive({
  num: [
    {
      required: true,
      message: '规格数量不能为空',
      trigger: 'blur'
    }
  ],
  oldPrice: [
    {
      required: true,
      message: '原价不能为空',
      trigger: 'blur'
    }
  ],
  price: [
    {
      required: true,
      message: '现价不能为空',
      trigger: 'blur'
    }
  ],
  expireTime: [
    {
      required: true,
      message: '有效期不能为空',
      trigger: 'blur'
    }
  ]
})
const skuFormRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, productId?: number, skuId?: number) => {
  formDialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  skuFormData.value.type = 1
  resetForm()
  const data = await getProduct(productId)
  productObj.value.name = data.name
  productObj.value.unitPrice = data.price
  productObj.value.id = data.id
  // 修改时，设置数据
  if (skuId) {
    skuFormLoading.value = true
    try {
      skuFormData.value = await ProductSkuApi.getProductSku(skuId)
    } finally {
      skuFormLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!skuFormRef) return
  const valid = await skuFormRef.value.validate()
  if (!valid) return
  // 提交请求
  skuFormLoading.value = true
  try {
    const data = skuFormData.value as unknown as ProductApi.ProductSkuVO
    data.productId = productObj.value.id
    data.name = productObj.value.name

    if (formType.value === 'create') {
      await ProductSkuApi.createProductSku(data)
      message.success(t('common.createSuccess'))
    } else {
      await ProductSkuApi.updateProductSku(data)
      message.success(t('common.updateSuccess'))
    }
    formDialogVisible.value = false
    // 发送操作成功的事件
    emit('success', productObj.value.id)
  } finally {
    skuFormLoading.value = false
  }
}
const typeChange = (val) => {
  if (val == 2) {
    skuFormData.value.price = productObj.value.unitPrice
    skuFormData.value.num = 0
    skuFormData.value.oldPrice = 0
  }
}
const numChange = (val) => {
  skuFormData.value.oldPrice = ((val * 100 * productObj.value.unitPrice) / 100).toFixed(2)
  skuFormData.value.price = ((val * 100 * productObj.value.unitPrice) / 100).toFixed(2)
}
/** 重置表单 */
const resetForm = () => {
  skuFormData.value = {
    productId: undefined,
    name: undefined,
    num: undefined,
    type: 1,
    oldPrice: undefined,
    price: undefined,
    expireTime: undefined
  }
  skuFormRef.value?.resetFields()
}
</script>

<style lang="scss" scoped>
// 主题色变量
$primary-color: #0052cc;
$primary-light: rgba(0, 82, 204, 0.1);
$primary-border: rgba(0, 82, 204, 0.3);
$success-color: #67c23a;
$warning-color: #e6a23c;
$danger-color: #f56c6c;
$info-color: #909399;
$text-primary: #303133;
$text-regular: #606266;
$text-secondary: #909399;
$text-placeholder: #c0c4cc;
$border-color: #dcdfe6;
$border-light: #e4e7ed;
$bg-color: #f5f7fa;
$shadow-light: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
$shadow-base:
  0 2px 4px rgba(0, 0, 0, 0.12),
  0 0 6px rgba(0, 0, 0, 0.04);
$border-radius: 12px;
$border-radius-small: 8px;

/* 弹窗样式 */
.enhanced-dialog {
  :deep(.el-dialog) {
    border-radius: $border-radius;
    overflow: hidden;

    @media (max-width: 768px) {
      width: 95% !important;
      margin: 0 auto;
    }
  }

  :deep(.el-dialog__header) {
    padding: 0;
    border-bottom: none;
  }

  :deep(.el-dialog__body) {
    padding: 0;
  }

  :deep(.el-dialog__footer) {
    padding: 0;
    border-top: none;
  }

  :deep(.el-dialog__headerbtn) {
    top: 16px;
    right: 16px;
    z-index: 10;

    .el-dialog__close {
      color: white;
      font-size: 18px;

      &:hover {
        color: rgba(255, 255, 255, 0.8);
      }
    }
  }
}

/* 弹窗头部 */
.dialog-header {
  display: flex;
  align-items: center;
  padding: 20px 24px;
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  color: white;

  @media (max-width: 768px) {
    padding: 16px 20px;
    flex-direction: column;
    gap: 8px;
    text-align: center;
  }
}

.header-icon {
  width: 40px;
  height: 40px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: $border-radius-small;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;

  @media (max-width: 768px) {
    width: 36px;
    height: 36px;
    font-size: 16px;
  }
}

.header-content {
  flex: 1;
  margin-left: 12px;

  @media (max-width: 768px) {
    margin-left: 0;
  }
}

.dialog-title {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 4px 0;

  @media (max-width: 768px) {
    font-size: 16px;
  }
}

.dialog-subtitle {
  font-size: 13px;
  opacity: 0.9;
  margin: 0;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

/* 表单容器 */
.form-container {
  padding: 24px;
  background: white;
  max-height: 70vh;
  overflow-y: auto;

  @media (max-width: 768px) {
    padding: 16px;
    max-height: 60vh;
  }

  /* 滚动条美化 */
  &::-webkit-scrollbar {
    width: 6px;
  }

  &::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.05);
    border-radius: 3px;
  }

  &::-webkit-scrollbar-thumb {
    background: rgba(0, 82, 204, 0.3);
    border-radius: 3px;
    transition: all 0.3s ease;

    &:hover {
      background: rgba(0, 82, 204, 0.5);
    }
  }
}

.enhanced-form {
  .form-section {
    margin-bottom: 32px;

    &:last-child {
      margin-bottom: 0;
    }

    @media (max-width: 768px) {
      margin-bottom: 24px;
    }
  }

  .section-title {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 20px;
    padding-bottom: 12px;
    border-bottom: 2px solid $primary-light;
    font-size: 16px;
    font-weight: 600;
    color: $text-primary;

    @media (max-width: 768px) {
      font-size: 14px;
      margin-bottom: 16px;
      padding-bottom: 8px;
    }
  }

  .section-icon {
    color: $primary-color;
    font-size: 18px;

    @media (max-width: 768px) {
      font-size: 16px;
    }
  }

  .form-item {
    margin-bottom: 20px;

    @media (max-width: 768px) {
      margin-bottom: 16px;
    }

    :deep(.el-form-item__label) {
      font-weight: 600;
      color: $text-regular;
      padding-bottom: 8px;

      @media (max-width: 768px) {
        font-size: 14px;
        width: 100% !important;
        text-align: left !important;
        margin-bottom: 8px;
      }
    }

    :deep(.el-form-item__content) {
      @media (max-width: 768px) {
        margin-left: 0 !important;
      }
    }

    &.info-item {
      :deep(.el-form-item__content) {
        display: flex;
        align-items: center;
      }
    }
  }

  // H5适配：表单项垂直布局
  @media (max-width: 768px) {
    :deep(.el-form-item) {
      display: block;
    }
  }
}

/* 信息展示 */
.info-display {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  background: $primary-light;
  border-radius: $border-radius-small;
  border: 1px solid $primary-border;
}

.info-icon {
  font-size: 16px;
  color: $primary-color;
  flex-shrink: 0;
}

.info-text {
  color: $text-primary;
  font-weight: 600;
  font-size: 14px;

  @media (max-width: 768px) {
    font-size: 13px;
  }

  &.price-text {
    color: $danger-color;
    font-family: 'Courier New', monospace;
  }
}

/* 输入框样式 */
.enhanced-input {
  :deep(.el-input__wrapper) {
    border-radius: $border-radius-small;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;
    padding: 12px 16px;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &.is-focus {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
      border-color: $primary-color;
    }
  }
}

/* 单选按钮组样式 */
.enhanced-radio-group {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;

  @media (max-width: 768px) {
    flex-direction: column;
    gap: 12px;
  }
}

.enhanced-radio {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border: 2px solid $border-color;
  border-radius: $border-radius-small;
  transition: all 0.3s ease;
  cursor: pointer;

  &:hover {
    border-color: $primary-color;
    background: $primary-light;
  }

  :deep(.el-radio__input) {
    .el-radio__inner {
      border-color: $primary-border;
      transition: all 0.3s ease;

      &:hover {
        border-color: $primary-color;
      }
    }

    &.is-checked {
      .el-radio__inner {
        background-color: $primary-color;
        border-color: $primary-color;
      }
    }
  }

  :deep(.el-radio__label) {
    font-weight: 500;
    color: $text-regular;
    display: flex;
    align-items: center;
    gap: 4px;
  }

  &.is-checked {
    border-color: $primary-color;
    background: $primary-light;
  }

  @media (max-width: 768px) {
    width: 100%;
    justify-content: center;
  }
}

.radio-icon {
  font-size: 14px;
  color: $primary-color;
}

/* 弹窗底部 */
.dialog-footer {
  display: flex;
  justify-content: center;
  gap: 12px;
  padding: 20px 24px;
  background: #fafbff;
  border-top: 1px solid $border-light;

  @media (max-width: 768px) {
    padding: 16px 20px;
    flex-direction: column;
  }
}

.submit-btn {
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  border: none;
  border-radius: $border-radius-small;
  padding: 12px 24px;
  font-weight: 500;
  color: white;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 6px;

  &:hover:not(:disabled) {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 82, 204, 0.3);
  }

  &:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    transform: none;
  }

  @media (max-width: 768px) {
    width: 100%;
    justify-content: center;
  }

  .btn-icon {
    font-size: 14px;
  }
}

.cancel-btn {
  border: 2px solid $border-color;
  color: $text-regular;
  border-radius: $border-radius-small;
  padding: 12px 24px;
  font-weight: 500;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 6px;

  &:hover {
    border-color: $primary-color;
    color: $primary-color;
    background: $primary-light;
  }

  @media (max-width: 768px) {
    width: 100%;
    justify-content: center;
  }

  .btn-icon {
    font-size: 14px;
  }
}

/* 加载状态优化 */
:deep(.el-loading-mask) {
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(2px);

  .el-loading-spinner {
    .path {
      stroke: $primary-color;
    }
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .form-container {
    background: #374151;
  }

  .dialog-footer {
    background: #4b5563;
    border-color: #6b7280;
  }

  .section-title {
    color: #f9fafb;
    border-color: rgba(96, 165, 250, 0.3);
  }

  .enhanced-form {
    .form-item {
      :deep(.el-form-item__label) {
        color: #d1d5db;
      }
    }
  }

  .info-display {
    background: rgba(96, 165, 250, 0.1);
    border-color: rgba(96, 165, 250, 0.3);
  }

  .info-text {
    color: #f9fafb;

    &.price-text {
      color: #f87171;
    }
  }

  .enhanced-input {
    :deep(.el-input__wrapper) {
      background: #4b5563;
      border-color: #6b7280;

      .el-input__inner {
        color: #f9fafb;
      }
    }
  }

  .enhanced-radio {
    background: #4b5563;
    border-color: #6b7280;
    color: #f9fafb;

    &:hover {
      border-color: #60a5fa;
      background: rgba(96, 165, 250, 0.1);
    }

    :deep(.el-radio__input) {
      .el-radio__inner {
        border-color: rgba(96, 165, 250, 0.3);
        background: #4b5563;

        &:hover {
          border-color: #60a5fa;
        }
      }

      &.is-checked {
        .el-radio__inner {
          background-color: #60a5fa;
          border-color: #60a5fa;
        }
      }
    }

    :deep(.el-radio__label) {
      color: #f9fafb;
    }

    &.is-checked {
      border-color: #60a5fa;
      background: rgba(96, 165, 250, 0.1);
    }
  }

  .radio-icon {
    color: #60a5fa;
  }
}

/* 响应式优化 */
@media (max-width: 480px) {
  .dialog-header {
    padding: 12px 16px;
  }

  .form-container {
    padding: 12px;
  }

  .enhanced-form {
    .form-section {
      margin-bottom: 20px;
    }

    .section-title {
      font-size: 13px;
      margin-bottom: 12px;
      padding-bottom: 6px;
    }

    .form-item {
      margin-bottom: 12px;

      :deep(.el-form-item__label) {
        font-size: 13px;
      }
    }
  }

  .info-text {
    font-size: 12px;
  }

  .enhanced-radio-group {
    gap: 8px;
  }

  .enhanced-radio {
    padding: 6px 12px;
  }

  .dialog-footer {
    padding: 12px 16px;
    gap: 8px;
  }
}
</style>
